package dtm

import (
	"encoding/json"
	"fmt"
	"gitee.com/xuyiping_admin/pkg/logger/zaplog"
	"gitee.com/xuyiping_admin/pkg/xerr"
	"go.uber.org/zap"
	"kpt-dtm/model"
	"kpt-dtm/model/yyn"
)

// FormulationDataPush 配方数据推送
func (s *StoreEntry) FormulationDataPush(pastureId int64, yConfig *model.YConfig, yMqtt *model.YMqtt, resultData []*yyn.FormulationData) error {
	body := yyn.MqttMessage{
		ReqMethod: yMqtt.ReqMethod,
		RegCode:   yMqtt.RegCode,
		Command:   yMqtt.Command,
		ModelType: yMqtt.ModelType,
		ApiId:     yMqtt.ApiId,
		Param: &yyn.Param{
			FarmId:     yConfig.FarmNumber,
			Method:     FormulationMethod,
			Code:       "1",
			ErrMessage: "",
			RowCount:   fmt.Sprintf("%d", len(resultData)),
			ResultData: resultData,
		},
	}
	b, _ := json.Marshal(body)
	if token := s.MqttManager.Publish(fmt.Sprintf("%d", pastureId), string(b)); token.Wait() && token.Error() != nil {
		return xerr.WithStack(token.Error())
	}
	return nil
}

// DryMatterDataPush 干物质数据推送
func (s *StoreEntry) DryMatterDataPush(pastureId int64, yConfig *model.YConfig, yMqtt *model.YMqtt, resultData []*yyn.DryMatter) error {
	body := yyn.MqttMessage{
		ReqMethod: yMqtt.ReqMethod,
		RegCode:   yMqtt.RegCode,
		Command:   yMqtt.Command,
		ModelType: yMqtt.ModelType,
		ApiId:     yMqtt.ApiId,
		Param: &yyn.Param{
			FarmId:     yConfig.FarmNumber,
			Method:     DryMatterMethod,
			Code:       "1",
			ErrMessage: "",
			RowCount:   fmt.Sprintf("%d", len(resultData)),
			ResultData: resultData,
		},
	}
	b, _ := json.Marshal(body)
	if token := s.MqttManager.Publish(fmt.Sprintf("%d", pastureId), string(b)); token.Wait() && token.Error() != nil {
		return xerr.WithStack(token.Error())
	}
	return nil
}

// MixingDataPush 混合数据推送
func (s *StoreEntry) MixingDataPush(pastureId int64, yConfig *model.YConfig, yMqtt *model.YMqtt, resultData []*yyn.MixData) error {
	body := yyn.MqttMessage{
		ReqMethod: yMqtt.ReqMethod,
		RegCode:   yMqtt.RegCode,
		Command:   yMqtt.Command,
		ModelType: yMqtt.ModelType,
		ApiId:     yMqtt.ApiId,
		Param: &yyn.Param{
			FarmId:     yConfig.FarmNumber,
			Method:     MixMethod,
			Code:       "1",
			ErrMessage: "",
			RowCount:   fmt.Sprintf("%d", len(resultData)),
			ResultData: resultData,
		},
	}
	b, _ := json.Marshal(body)
	if token := s.MqttManager.Publish(fmt.Sprintf("%d", pastureId), string(b)); token.Wait() && token.Error() != nil {
		return xerr.WithStack(token.Error())
	}
	return nil
}

// SpreadDataPush 撒料数据推送
func (s *StoreEntry) SpreadDataPush(pastureId int64, yConfig *model.YConfig, yMqtt *model.YMqtt, resultData []*yyn.UploadDiliverData) error {
	body := yyn.MqttMessage{
		ReqMethod: yMqtt.ReqMethod,
		RegCode:   yMqtt.RegCode,
		Command:   yMqtt.Command,
		ModelType: yMqtt.ModelType,
		ApiId:     yMqtt.ApiId,
		Param: &yyn.Param{
			FarmId:     yConfig.FarmNumber,
			Method:     SpreadMethod,
			Code:       "1",
			ErrMessage: "",
			RowCount:   fmt.Sprintf("%d", len(resultData)),
			ResultData: resultData,
		},
	}
	b, _ := json.Marshal(body)
	zaplog.Info("SpreadMethod", zap.Any("body", body))
	if token := s.MqttManager.Publish(fmt.Sprintf("%d", pastureId), string(b)); token.Wait() && token.Error() != nil {
		return xerr.WithStack(token.Error())
	}
	return nil
}
